package com.hxzy.controller.back;

import com.hxzy.common.annons.Admin;
import com.hxzy.common.annons.Shop;
import com.hxzy.config.WaimaiThreadLocal;
import com.hxzy.entity.EnterpriseProcess;
import com.hxzy.vo.back.AdminLoginVO;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hxzy.common.controller.BaseController;
import com.hxzy.common.vo.PageVO;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Date;
import javax.validation.Valid;
import com.hxzy.dto.back.MerchandiserSearchDTO;
import com.hxzy.entity.Merchandiser;
import com.hxzy.service.MerchandiserService;
import java.math.BigDecimal;

/**
* @Author 商家信息
* @Description TODO
* @Date 2023/4/6 17:11
* @Version 1.0
*/
@Api(tags = "商家信息API")
@RestController
@RequestMapping(value = "/api/merchandiser")
public class EnterpriseController   extends BaseController {


    @Autowired
    private MerchandiserService  merchandiserService;

    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/data")
    public R<PageVO<Merchandiser>>  search(MerchandiserSearchDTO  searchDTO){
        //开启分页
        PageHelper.startPage(searchDTO.getPage(),searchDTO.getSize());
        //普通查询
        List<Merchandiser> arr=this.merchandiserService.search(searchDTO);
        //转换成自己的对象
        return super.pageToPageVO(arr);
    }



    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{id}")
    public R<Merchandiser> findById(@PathVariable(value = "id") BigDecimal id){
         Merchandiser db=this.merchandiserService.selectByPrimaryKey(id);
        if(db==null){
           return R.build(AckCode.NOT_FOUND_DATA);
        }
        return R.okHasData(db);
    }

    @ApiOperation(value = "导出查询数据到excel")
    @PostMapping(value = "/download")
    public void downloadExcel(@RequestBody MerchandiserSearchDTO  searchDTO , HttpServletResponse response) throws IOException {
        //普通查询
        List<Merchandiser> arr=this.merchandiserService.search(searchDTO);

        String fileName = "Merchandiser_data_" + System.currentTimeMillis() + ".xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Merchandiser.class).sheet("result").doWrite(arr);
    }


    @Shop
    @ApiOperation(value = "修改")
    @PutMapping
    public R  update(@RequestBody @Valid Merchandiser entity) {
        AdminLoginVO adminLoginVO = WaimaiThreadLocal.adminThreadLocal.get();

        Merchandiser db=this.merchandiserService.selectByPrimaryKey(entity.getId());
        if(db==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        //只允许修改的电话、地址、经纬 度
        db.setPhone(entity.getPhone());
        db.setPicture(entity.getPicture());
        db.setLatitude(entity.getLatitude());
        db.setLongitude(entity.getLongitude());
        db.setAddress(entity.getAddress());

        db.setUpdateTime(new Date());
        db.setUpdateBy(adminLoginVO.getName());
        //执行
        int count = this.merchandiserService.updateByPrimaryKeySelective(entity);
        return  super.toR(count);
    }

    @Admin
    @ApiOperation(value = "管理员审核商家")
    @PutMapping(value = "/process")
    public R  process(@RequestBody @Valid EnterpriseProcess entity) {
         boolean result=this.merchandiserService.process(entity);
        return  super.toR(result);
    }

}